package com.halflife.framework.generator;

import com.halflife.framework.generator.build.SimpleBuilder;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created by zhaoyf on 2018/6/18.
 */
public class GenerateUtil {
    public static ResourceBundle resource =  ResourceBundle.getBundle("templates/generator-mysql");

    public static void mkDir(File file) {
        if (file.exists()) {
            return;
        }
        System.out.println(file.getParentFile());
        mkDir(file.getParentFile());

        file.mkdir();

    }
    public static void setFieldsBySequence(Class clazz, Map<String,Field> fieldMap, List<Field> fields) {
        List<Field> objectList = new ArrayList<>();
        setFields(clazz,fieldMap,fields,objectList);
        for (Field field : objectList) {
            if (!fieldMap.containsKey(field.getName())) {
                fieldMap.put(field.getName(), field);
                fields.add(field);

            }
        }
    }
    public static void setFields(Class clazz, Map<String,Field> fieldMap,List<Field> fields,List<Field> objectList) {
        for (Field field : clazz.getDeclaredFields()) {
            if (Modifier.isProtected(field.getModifiers())||Modifier.isPrivate(field.getModifiers())) {
                if (!isPrimitive(field)) {
                    objectList.add(field);
                    continue;
                }
                if (!fieldMap.containsKey(field.getName())) {
                    fieldMap.put(field.getName(), field);
                    if (field.getName().equals("id")) {
                        fields.add(0,field);
                    }else{
                        fields.add(field);
                    }

                }
            }
        }
        Class superClass= clazz.getSuperclass();
        if (superClass == Object.class) {
            return;
        }
        setFields(superClass,fieldMap,fields,objectList);
    }

    public static boolean isPrimitive(Field field) {
        Class clazz = field.getType();
        if (clazz == Integer.class || clazz == int.class) {
            return true;
        }
        if (clazz == String.class) {
            return true;
        }
        if (clazz == Long.class || clazz == long.class) {
            return true;
        }
        if (clazz == Date.class ) {
            return true;
        }
        if (clazz == Short.class || clazz == short.class) {
            return true;
        }
        if (clazz == Byte.class || clazz == byte.class) {
            return true;
        }
        if (clazz == Double.class || clazz == double.class) {
            return true;
        }
        if (clazz == Boolean.class || clazz == boolean.class) {
            return true;
        }
        if (clazz == BigDecimal.class ) {
            return true;
        }
        return false;
    }


    public static String getAppLocation() {
        String path = SimpleBuilder.class.getResource("").getPath().substring(1).split("/classes")[0];
        Integer positon = path.lastIndexOf("/");
        return "/"+path.substring(0, positon);
    }

    public static String getJoinCondition(String type) {
        if (StringUtils.isBlank(type)) {
            return "left outer join";
        }
        if (type.equals("L") || type.equals("l")) {
            return "left outer join";
        }
        if (type.equals("I") || type.equals("i")) {
            return "inner join";
        }
        if (type.equals("R") || type.equals("r")) {
            return "right outer join";
        }
        return "left outer join";
    }
    public static String getDbType(String field) {
        return resource.getString("type." + field);
    }

}
